﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace 封包测试工具
{
    /// <summary>
    /// 编码转换类
    /// </summary>
    public class EncodeConver
    {
        public string Encode(string type, string code,string encoding)
        {
            switch (type)
            {
                case "URL": return EncodeUrl(code, encoding);
                case "\\ 转 \\\\": return RegexCode(code);
                case "Base64":return EncodeBase64(code);
                case "Html": return EncodeHtml(code);
                case "Html2": return EncodeHtml2(code);
                case "Unicode": return EncodeUnicode(code);
                case "Md5":return EncodeMD5(code);
                    
                default:return "未知的方式" + type;
            }
        }
        public string Decode(string type, string code,string encoding)
        {
            switch (type)
            {
                case "URL": return DecodeUrl(code,encoding);
                case "\\ 转 \\\\": return RegexDecode(code);
                case "Base64": return DecodeBase64(code);
                case "Html": return DecodeHtml(code);
                case "Html2": return DecodeHtml2(code);
                case "Unicode": return DecodeUnicodeString(code);
                default: return "未知的方式" + type;
            }
        }
        #region Url
        private string EncodeUrl(string url, string encoding)
        {
            return HttpUtility.UrlEncode(url, Encoding.GetEncoding(encoding));
        }
        private string DecodeUrl(string url, string encoding)
        {
            return HttpUtility.UrlDecode(url, Encoding.GetEncoding(encoding));
        }
        #endregion
        #region Base64
        private string EncodeBase64(string param)
        {
            byte[] buffer = Encoding.Default.GetBytes(param);
            try
            {
                return Convert.ToBase64String(buffer);
            }
            catch
            {
                return "Base64 编码错误";
            }
        }
        private string DecodeBase64(string param)
        {
            try
            {
                return Encoding.Default.GetString(Convert.FromBase64String(param));
            }
            catch (Exception)
            {

                return "Base64 解码错误";
            }
            
        }
        #endregion
        #region Regex
        private string RegexCode(string RegexStr)
        {
            string temp = RegexStr;
            temp = temp.Replace("\\", "\\\\");
            temp = temp.Replace("\"", "\\\"");
            return temp;
        }
        private string RegexDecode(string RegexStr)
        {
            string temp = RegexStr;
            temp = temp.Replace("\\\"", "\"");
            temp = temp.Replace("\\\\", "\\");
            return temp;
        }
        #endregion
        #region Html
        private string EncodeHtml(string code)
        {
            return System.Web.HttpUtility.HtmlEncode(code);
        }
        private string DecodeHtml(string code)
        {
            return System.Web.HttpUtility.HtmlDecode(code);
        }
        #endregion
        #region Html2
        private string EncodeHtml2(string code)
        {
            string r = string.Empty;
            for(int i=0; i < code.Length; i++)
            {
                //System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[\\u4e00-\\u9fa5]");
                //if (reg.IsMatch(code.Substring(i, 1)))
                //{
                //    r += "&#" + Char.ConvertToUtf32(code, i) + ";";
                //}
                //else
                //{
                //    r += code.Substring(i, 1);
                //}
                r += "&#" + Char.ConvertToUtf32(code, i) + ";";
            }
            return r;

        }
        private string DecodeHtml2(string code)
        {
            return System.Web.HttpUtility.HtmlDecode(code);
        }
        #endregion
        #region Unicode
        /// <summary>
        /// Unicode编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string EncodeUnicode(string str)
        {
            StringBuilder strResult = new StringBuilder();
            if (!string.IsNullOrEmpty(str))
            {
                for (int i = 0; i < str.Length; i++)
                {
                    strResult.Append("\\u");
                    strResult.Append(((int)str[i]).ToString("x"));
                }
            }
            return strResult.ToString();
        }

        /// <summary>
        /// Unicode字符转汉字. 
        /// 默认处理 \u7384\u673a\u8bba\u575b 
        /// 如果遇到 &7384 &673a &8bba &575b  请对SplitString赋值为&
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <param name="SplitString">默认为u</param>
        /// <returns></returns>
        private string DecodeUnicodeString(string str, string SplitString = "u")
        {
            string regexCode = SplitString == "u" ? "\\\\u(\\w{1,4})" : SplitString + "(\\w{1,4})";
            string reString = str;
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regexCode);
            System.Text.RegularExpressions.MatchCollection mc = reg.Matches(reString);
            for (int i = 0; i < mc.Count; i++)
            {
                try
                {
                    var outs = (char)int.Parse(mc[i].Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
                    str = str.Replace(mc[i].Groups[0].Value, outs.ToString());
                }
                catch
                {
                    continue;
                }
            }
            return str;
        }
        #endregion
        #region Md5
       
        public string EncodeMD5(string param)
        {
            byte[] bytes = Encoding.Default.GetBytes(param);
            MD5 md5 = MD5.Create();
            byte[] buffer = md5.ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < buffer.Length; i++)
            {
                sb.Append(buffer[i].ToString("x2"));
            }
            return sb.ToString();
        }

        #endregion




    }
}
